package src;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.interactions.Actions;

import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

public class VideoWatchTest {
    private WebDriver driver;
    private WebDriverWait wait;

    

    public VideoWatchTest(WebDriver driver) {
        this.driver = driver;
        this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
    }

    public void switchToNewestPage(WebDriver driver) {
        Set<String> windowHandles = driver.getWindowHandles();

        List<String> handlesList = new ArrayList<>(windowHandles);
        String newestHandle = handlesList.get(handlesList.size() - 1);

        driver.switchTo().window(newestHandle);
        System.out.println("Switched to the newest window/tab: " + driver.getTitle());
    }


    public void testWatch() throws InterruptedException {
        // 访问待测试网站
        driver.get("https://www.bilibili.com");
        
        // 输入内容并点击搜索
        WebElement navSearchInput = driver.findElement(By.cssSelector(".nav-search-input"));
        if (!navSearchInput.isDisplayed()) {
            throw new RuntimeException("Error: .nav-search-input is not visible!");
        }
        navSearchInput.sendKeys("Python入门");
        Thread.sleep(500);
        WebElement navSearchButton = driver.findElement(By.cssSelector(".nav-search-btn"));
        if (!navSearchButton.isDisplayed()) {
            throw new RuntimeException("Error: .nav-search-btn is not visible!");
        }
        wait.until(ExpectedConditions.elementToBeClickable(navSearchButton)).click();
        Thread.sleep(1000);


        // 将Selenium控制转移到新开启的窗口当中
        switchToNewestPage(driver);
        wait.until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("document.body.style.zoom = '0.5'"); // 进行适当缩放从而呈现更多筛选后的结果
        Thread.sleep(2000);

        // 视频点击与观看
        WebElement firstVideoLink = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(".bili-video-card__wrap a")));
        String href = firstVideoLink.getDomAttribute("href");

        if (href != null && !href.isEmpty()) {
            String videoUrl = href.startsWith("//") ? "https:" + href : href;
            System.out.println("Navigating to video URL: " + videoUrl);
            driver.get(videoUrl);
            wait.until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
            System.out.println("Successfully navigated to the video page!");
        } else {
            throw new RuntimeException("Error: Could not extract video URL.");
        }

        Thread.sleep(2000);
        // 将Selenium控制转移到新开启的窗口当中
        switchToNewestPage(driver);
        js.executeScript("document.body.style.zoom = '0.5'");
        Thread.sleep(4000);

        // 将鼠标移动到视频画面上触发控件显示
        WebElement videoFrame = driver.findElement(By.tagName("video"));
        Actions actions = new Actions(driver);
        actions.moveToElement(videoFrame).perform();

        // 进入网页全屏
        WebElement webFullScreenButton = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector(".bpx-player-ctrl-btn.bpx-player-ctrl-web")));
        webFullScreenButton.click();
        Thread.sleep(1000);

        // 进入浏览器全屏
        actions.moveToElement(videoFrame).perform();
        WebElement browserFullScreenButton = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector(".bpx-player-ctrl-btn.bpx-player-ctrl-full")));
        browserFullScreenButton.click();
        Thread.sleep(2000);

        // 快进与回退操作
        actions.sendKeys(Keys.ARROW_RIGHT).perform();
        Thread.sleep(1000);
        actions.sendKeys(Keys.ARROW_LEFT).perform();
        Thread.sleep(1000);

        // 播放速度测试
        actions.moveToElement(videoFrame).perform();
        WebElement playbackSpeedButton = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector(".bpx-player-ctrl-playbackrate-result")));
        playbackSpeedButton.click();
        Thread.sleep(1000);
        WebElement doubleSpeedOption = wait.until(ExpectedConditions.visibilityOfElementLocated(
            By.cssSelector(".bpx-player-ctrl-playbackrate-menu > [data-value='2']")));
        doubleSpeedOption.click();
        Thread.sleep(2000);

        // 音量调节测试
        actions.sendKeys(Keys.ARROW_UP).perform(); 
        Thread.sleep(1000);
        actions.sendKeys(Keys.ARROW_DOWN).perform(); 
        Thread.sleep(1000);
    }
}
